package com.liu.eduservice.controller;


import com.liu.commonutils.ReturnResult;
import com.liu.eduservice.entity.EduChapter;
import com.liu.eduservice.entity.EduVideo;
import com.liu.eduservice.service.EduVideoService;
import com.liu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @since 2022-05-20
 */
@RestController
@RequestMapping("/eduservice/video")
@Api(description = "课程小节管理")
@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService eduVideoService;


    // 增加小节
    @ApiOperation("增加小节")
    @PostMapping("/addVideo")
    public ReturnResult addVideo(@RequestBody EduVideo eduVideo) {
        try {
            boolean save = eduVideoService.save(eduVideo);
            if (!save) {
                return ReturnResult.error();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ReturnResult.ok();
    }

    // 删除小节信息
    @ApiOperation("删除小节")
    @DeleteMapping("/deletedVideoById/{videoId}")
    public ReturnResult deletedVideo(@PathVariable String videoId) {
        /*删除小节做判断
         * 1.小节中没有视频教程，直接删除小节
         * 2.小节中有视频教程
         * 2.2 先删除视频教程，在删除小节信息
         * */

        // 第一步，删除视频
        eduVideoService.deletedVideoByVideoId(videoId);

        // 第二步，删除小节
        try {
            boolean b = eduVideoService.removeById(videoId);
            if (!b) {
                return ReturnResult.error();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ReturnResult.ok();
    }

    // 通过id查询小节，做信息回显
    @ApiOperation("通过id查询小节")
    @GetMapping("/queryVideoInfoById/{videoId}")
    public ReturnResult queryVideoInfoById(@PathVariable String videoId) {
        EduVideo byId = eduVideoService.getById(videoId);
        return ReturnResult.ok().data("videoInfo", byId);
    }

    // 根据上面查询video信息，进行数据修改
    @PostMapping("/updateVideo")
    @ApiOperation("修改小节信息")
    public ReturnResult updateChapterById(@RequestBody EduVideo eduVideo) {
        try {
            boolean updateById = eduVideoService.updateById(eduVideo);
            if (!updateById) {
                return ReturnResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnResult.ok();
    }

}

